﻿<UserControl x:Class="PrestoDashboard.Tabs.ApplicationServerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             xmlns:ff="clr-namespace:FunctionalFun.UI.Behaviours;assembly=PrestoCommon"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:galaSoft="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="860">
    <UserControl.DataContext>
        <Binding Source="{StaticResource ApplicationServerViewModel}"/>
    </UserControl.DataContext>    

    <!-- This works for the initial sort, but when members get added to the collection
         the sort doesn't get updated. That's because CollectionViewSource doesn't 
         implement INotifyPropertyChanged. -->
    <UserControl.Resources>

        <CollectionViewSource x:Key="SortedServers" Source="{Binding ApplicationServers}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Name" Direction="Ascending"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

    </UserControl.Resources>    
    
    <Grid IsEnabled="{Binding ServersLoaded}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="275"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        
        <TextBlock Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center"
                   FontSize="24" Visibility="{Binding ShowWaitCursor}" Panel.ZIndex="1"
                   Foreground="White">Loading...
        </TextBlock>
        
        <DockPanel Grid.Column="0" LastChildFill="True">
            <Label DockPanel.Dock="Top" Style="{StaticResource StandardLabel}" Content="Application Servers" HorizontalAlignment="Center" FontWeight="Bold"/>
            <StackPanel DockPanel.Dock="Bottom" Orientation="Vertical" Height="40" HorizontalAlignment="Center">
                <StackPanel Orientation="Horizontal">
                    <Button Style="{StaticResource StandardButton}" Content="Add" Command="{Binding AddServerCommand}" />
                    <Button Style="{StaticResource StandardButton}" Content="Refresh" Command="{Binding RefreshServersCommand}" />
                    <CheckBox Content="Show all" VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding ShowAllServers}" />
                    <!--<Button Style="{StaticResource StandardButton}" Content="Delete..." Command="{Binding DeleteServerCommand}" />-->
                </StackPanel>
            </StackPanel>

            <DockPanel DockPanel.Dock="Bottom" LastChildFill="True">
                <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding InstallationEnvironments}"
                          SelectedItem="{Binding SelectedInstallationEnvironment}" />
                <ListBox ItemsSource="{Binding Source={StaticResource SortedServers}}" DisplayMemberPath="Name"
                     SelectedItem="{Binding SelectedApplicationServer}" />
            </DockPanel>            
        </DockPanel>

        <DockPanel Grid.Column="1" LastChildFill="True" IsEnabled="{Binding AppServerIsSelected}">            
                        
            <Grid DockPanel.Dock="Top">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="40"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="140"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource StandardLabel}" Content="Selected Application Server" HorizontalAlignment="Center" FontWeight="Bold"/>
                <CheckBox Grid.Row="0" Grid.ColumnSpan="2" Content="Archived" IsChecked="{Binding SelectedApplicationServer.Archived}" HorizontalAlignment="Right" VerticalAlignment="Center"/>

                <Label Grid.Column="0" Grid.Row="1" Style="{StaticResource StandardLabel}" Content="Name: "/>
                <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding SelectedApplicationServer.Name}"/>

                <Label Grid.Column="0" Grid.Row="2" Style="{StaticResource StandardLabel}" Content="Description: "/>
                <TextBox  Grid.Column="1" Grid.Row="2" Text="{Binding SelectedApplicationServer.Description}"/>

                <Label Grid.Column="0" Grid.Row="3" Style="{StaticResource StandardLabel}" Content="Environment: "/>

                <DockPanel Grid.Column="1" Grid.Row="3">
                    <ComboBox ItemsSource="{Binding InstallationEnvironments}"
                              SelectedItem="{Binding SelectedApplicationServerInstallationEnvironment}" Width="110"
                              HorizontalAlignment="Left"/>
                    <CheckBox Style="{StaticResource StandardCheckBox}" Content="Enable debug logging" IsChecked="{Binding SelectedApplicationServer.EnableDebugLogging}"
                              FlowDirection="LeftToRight" VerticalAlignment="Center" Width="140" HorizontalAlignment="Right"/>
                </DockPanel>

                <StackPanel Grid.Row="4" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center">
                    <Button Style="{StaticResource StandardButton}" Content="Save" Command="{Binding SaveServerCommand}" />
                </StackPanel>
            </Grid>                        
            
            <DockPanel DockPanel.Dock="Bottom">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="25"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="40"/>
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0" Style="{StaticResource StandardLabel}" Content="Custom Variable Groups" FontWeight="Bold"/>
                    <DataGrid Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True" HeadersVisibility="Column"
                          ItemsSource="{Binding SelectedApplicationServerCustomVariableGroups}"
                          SelectedItem="{Binding SelectedCustomVariableGroup}"
                          Height="125">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="*"/>
                        </DataGrid.Columns>
                    </DataGrid>
                    <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" IsEnabled="{Binding UserCanAlter}">
                        <Button Style="{StaticResource StandardButton}" Content="Add..." Command="{Binding AddVariableGroupCommand}" />
                        <Button Style="{StaticResource StandardButton}" Content="Remove..." Command="{Binding RemoveVariableGroupCommand}" />
                    </StackPanel>
                </Grid>
            </DockPanel>                       

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="40"/>
                </Grid.RowDefinitions>
                <Label Grid.Row="0" Style="{StaticResource StandardLabel}" Content="Applications" FontWeight="Bold"/>
                <DataGrid Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True" HeadersVisibility="Column"
                          ItemsSource="{Binding SelectedApplicationServerApplicationsWithOverrideGroup}"
                          ff:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedApplicationsWithOverrideGroup}"
                          SelectionMode="Extended">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <galaSoft:EventToCommand Command="{Binding EditApplicationCommand, Mode=OneWay}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Path=Application.Name}" Header="Name" Width="150" />
                        <DataGridTextColumn Binding="{Binding Path=Application.Version}" Header="Version" Width="100"/>
                        <DataGridTextColumn Binding="{Binding Path=CustomVariableGroupNames}" Header="Override Group" Width="*"/>
                        <DataGridTextColumn Binding="{Binding Path=Enabled}" Header="Enabled" Width="60"/>
                    </DataGrid.Columns>
                </DataGrid>

                <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" IsEnabled="{Binding UserCanAlter}">
                    <Button Style="{StaticResource StandardButton}" Content="Add..." Command="{Binding AddApplicationCommand}" />
                    <Button Style="{StaticResource StandardButton}" Content="Edit..." Command="{Binding EditApplicationCommand}" />
                    <Button Style="{StaticResource StandardButton}" Content="Remove..." Command="{Binding RemoveApplicationCommand}" />
                    <Button Style="{StaticResource StandardButton}" Content="Import..." Command="{Binding ImportApplicationCommand}" />
                    <Button Style="{StaticResource StandardButton}" Content="Export..." Command="{Binding ExportApplicationCommand}" />
                </StackPanel>

                <!-- This is separate because we always want it enabled. -->
                <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
                    <Button Style="{StaticResource StandardButton}" Content="Install..." Command="{Binding ForceApplicationCommand}" />
                </StackPanel>
            </Grid>
            
        </DockPanel>

    </Grid>
</UserControl>
